sequenceDiagram
    title 服务购买-发起支付

    autonumber

    actor User as 用户
    participant DealerServicePurchaseOrderApplicationService

    participant DealerServicePurchaseOrderRepository
    participant DealerServicePurchaseOrder
    participant InitiatePaymentService
    participant PaymentOrder
    participant PaymentServiceSelector
    participant PaymentService
    participant PaymentOrderRepository

    participant DomainEventPublisher

User->>DealerServicePurchaseOrderApplicationService: initiatePayment()

    activate DealerServicePurchaseOrderApplicationService

    DealerServicePurchaseOrderApplicationService->>DealerServicePurchaseOrderRepository: findById(DealerServicePurchaseOrderId)

    activate DealerServicePurchaseOrderRepository

    DealerServicePurchaseOrderRepository->>DealerServicePurchaseOrder: 查询 DealerServicePurchaseOrder 并重建

    activate DealerServicePurchaseOrder
    DealerServicePurchaseOrder->>DealerServicePurchaseOrder: new DealerServicePurchaseOrder(...)
    DealerServicePurchaseOrder-->>DealerServicePurchaseOrderRepository: DealerServicePurchaseOrder
    deactivate DealerServicePurchaseOrder

    DealerServicePurchaseOrderRepository-->>DealerServicePurchaseOrderApplicationService: DealerServicePurchaseOrder

    deactivate DealerServicePurchaseOrderRepository

    DealerServicePurchaseOrderApplicationService->>InitiatePaymentService: initiatePayment(order)

    activate InitiatePaymentService

    InitiatePaymentService->>DealerServicePurchaseOrder: status() 获取订单状态
    activate DealerServicePurchaseOrder
    DealerServicePurchaseOrder-->>InitiatePaymentService: status
    deactivate DealerServicePurchaseOrder

    alt 若订单当前状态非未发起支付、已发起支付
    InitiatePaymentService->>InitiatePaymentService: 中断流程
    end

    InitiatePaymentService->>PaymentOrderRepository: findByOrder(orderType, orderId) 获取订单全部支付单
    activate PaymentOrderRepository
    PaymentOrderRepository-->>InitiatePaymentService: 订单全部支付单
    deactivate PaymentOrderRepository

    alt 若订单订单存在未发起支付、已发起支付、支付失败三种状态外的支付单
    InitiatePaymentService->>InitiatePaymentService: 中断流程
    end

    loop 检查所有已发起支付支付单实时状态
        InitiatePaymentService->>PaymentServiceSelector: findPaymentServiceByChannelType(paymentChannelType) 获取具体的支付领域服务
        activate PaymentServiceSelector
        PaymentServiceSelector-->>InitiatePaymentService: PaymentService
        deactivate PaymentServiceSelector

        InitiatePaymentService->>PaymentService: queryPaymentStatus(PaymentOrder)
        activate PaymentService
        PaymentService-->>InitiatePaymentService: 支付单实时状态
        deactivate PaymentService

        alt 若支付单实时查询状态非已发起支付
            InitiatePaymentService->>InitiatePaymentService: 中断流程
        end
    end


    InitiatePaymentService->>InitiatePaymentService: 筛选符合当前支付渠道和类型的已存在的支付单

    alt 如不存在已发起过的支付单
    InitiatePaymentService->>PaymentOrder: 创建新的支付单
    activate PaymentOrder
    PaymentOrder->>PaymentOrder: new PaymentOrder(...)
    PaymentOrder-->>InitiatePaymentService: PaymentOrder
    deactivate PaymentOrder

    end

    InitiatePaymentService->>PaymentServiceSelector: findPaymentServiceByChannelType(paymentChannelType) 获取具体的支付领域服务
    activate PaymentServiceSelector
    PaymentServiceSelector-->>InitiatePaymentService: PaymentService
    deactivate PaymentServiceSelector
    InitiatePaymentService->>PaymentOrder: initiatePayment(PaymentService)
    activate PaymentOrder
    PaymentOrder->>PaymentService: initiatePayment(PaymentOrder)
    activate PaymentService
    PaymentService-->>PaymentOrder: InitiatePaymentResult
    deactivate PaymentService
    PaymentOrder->>DomainEventPublisher: 发布领域事件 PaymentInitiated
    PaymentOrder-->>InitiatePaymentService: InitiatePaymentResult
    deactivate PaymentOrder

    InitiatePaymentService->>PaymentOrderRepository: save(PaymentOrder)

    InitiatePaymentService-->>DealerServicePurchaseOrderApplicationService: InitiatePaymentResult
    deactivate InitiatePaymentService

    deactivate DealerServicePurchaseOrderApplicationService